__author__ = 'st316'
'''
Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally
or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[ "ABCE",
  "SFCS",
  "ADEE"
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

注：DFS ,由于每个字母只能用一次，所以需要标注已经走过的点
'''


class Solution:
    # @param board, a list of lists of 1 length string
    # @param word, a string
    # @return a boolean
    def exist(self, board, word):
        # board = [x[0] for x in board]
        flag = False
        if not word or not board:
            return flag
        for i in xrange(len(board)):
            for j in xrange(len(board[i])):
                if board[i][j] == word[0]:
                    usedNode = [(i, j)]
                    flag = (self.searchUp(board, word, i, j, 1, usedNode)
                            or self.searchDown(board, word, i, j, 1, usedNode)
                            or self.searchLeft(board, word, i, j, 1, usedNode)
                            or self.searchRight(board, word, i, j, 1, usedNode))
                    if flag:
                        return flag
        return flag

    def searchUp(self, board, word, i, j, n, usedNode):
        if n == len(word):
            return True
        if i - 1 < 0:
            return False
        if (i - 1, j) in usedNode:
            return False
        if board[i - 1][j] == word[n]:
            usedNode.append((i - 1, j))
            flag = (self.searchUp(board, word, i - 1, j, n + 1, usedNode)
                    or self.searchLeft(board, word, i - 1, j, n + 1, usedNode)
                    or self.searchRight(board, word, i - 1, j, n + 1, usedNode))
            if not flag:
                usedNode.pop()
            return flag
        else:
            return False

    def searchDown(self, board, word, i, j, n, usedNode):
        if n == len(word):
            return True
        if i + 1 > len(board) - 1:
            return False
        if (i + 1, j) in usedNode:
            return False
        if board[i + 1][j] == word[n]:
            usedNode.append((i + 1, j))
            flag = (self.searchDown(board, word, i + 1, j, n + 1, usedNode)
                    or self.searchLeft(board, word, i + 1, j, n + 1, usedNode)
                    or self.searchRight(board, word, i + 1, j, n + 1, usedNode))
            if not flag:
                usedNode.pop()
            return flag
        else:
            return False

    def searchLeft(self, board, word, i, j, n, usedNode):
        if n == len(word):
            return True
        if j - 1 < 0:
            return False
        if (i, j - 1) in usedNode:
            return False
        if board[i][j - 1] == word[n]:
            usedNode.append((i, j - 1))
            flag = (self.searchUp(board, word, i, j - 1, n + 1, usedNode)
                    or self.searchDown(board, word, i, j - 1, n + 1, usedNode)
                    or self.searchLeft(board, word, i, j - 1, n + 1, usedNode))
            if not flag:
                usedNode.pop()
            return flag
        else:
            return False

    def searchRight(self, board, word, i, j, n, usedNode):
        if n == len(word):
            return True
        if j + 1 > len(board[i]) - 1:
            return False
        if (i, j + 1) in usedNode:
            return False
        if board[i][j + 1] == word[n]:
            usedNode.append((i, j + 1))
            flag = (self.searchUp(board, word, i, j + 1, n + 1, usedNode)
                    or self.searchDown(board, word, i, j + 1, n + 1, usedNode)
                    or self.searchRight(board, word, i, j + 1, n + 1, usedNode))
            if not flag:
                usedNode.pop()
            return flag
        else:
            return False


if __name__ == '__main__':
    s = Solution()
    print s.exist(["ABCE", "SFES", "ADEE"], "ABCESEEEFS")